package org.komarichin.struts2.converters;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.util.StrutsTypeConverter;
import org.komarichin.beans.ILanguage;
import org.komarichin.beans.service.ILanguageService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * User: Mykhaylo Komarichyn
 * Date: 03.02.2010
 * Time: 15:02:39
 */
public class LanguageConverter extends StrutsTypeConverter {

    @Autowired
    private ILanguageService languageService;

    @Override
    public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values == null || values.length == 0) {
            return null;
        }
        return languageService.findByName(values[0]);
    }

    @Override
    public String convertToString(Map context, Object o) {
        if (o == null) return StringUtils.EMPTY;
        return ((ILanguage) o).getIsoName();

    }
}
